package com.example.mynews.UI;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.mynews.Adapter.Guide_pagerAdapter;
import com.example.mynews.R;

public class Guide extends AppCompatActivity{

    private ViewPager guide_viewpager;
    private Button guide_btn;
    private LinearLayout guide_dot;

    private int imge[]={R.mipmap.guide_1, R.mipmap.guide_2, R.mipmap.guide_3};
    private ImageView[] imagee;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();
        inifuction();
    }
    private void initView() {
        guide_viewpager = (ViewPager) findViewById(R.id.guide_viewpager);
        guide_btn = (Button) findViewById(R.id.guide_btn);
        guide_dot = (LinearLayout) findViewById(R.id.guide_dot);
    }
    private void inifuction() {
        //创建小点
        imagee=new ImageView[imge.length];
        for (int i = 0; i < imge.length; i++) {
            ImageView imageView=new ImageView(this);
            //设置布局参数
           LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(50,50);
           params.setMargins(20,0,20,0);
           imageView.setLayoutParams(params);
           if (i==0)
           {
               imageView.setBackgroundResource(R.mipmap.page_indicator_focused);
           }else
           {
               imageView.setBackgroundResource(R.mipmap.page_indicator_unfocused);
           }
           imagee[i]=imageView;
           guide_dot.addView(imageView);

        }
        //设置viewpager
        guide_viewpager.setAdapter(new Guide_pagerAdapter(imge));
        guide_viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i <imagee.length; i++) {
                    imagee[i].setBackgroundResource(R.mipmap.page_indicator_unfocused);
                }
                if (position==imge.length-1)
                {
                    guide_btn.setVisibility(View.VISIBLE);
                }else
                {
                    guide_btn.setVisibility(View.GONE);
                }
                imagee[position].setBackgroundResource(R.mipmap.page_indicator_focused);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        //设置按钮点击
        guide_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Guide.this, Index.class));
                finish();
            }
        });
    }

}